//
//  MXUpdateNickNameController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/4.
//

#import "MXUpdateNickNameController.h"
#import "MxPublicNav.h"
@interface MXUpdateNickNameController () <MxNavigationViewDelegate>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) UILabel *nextLab;
@end

@implementation MXUpdateNickNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.name = @"";
    [self createUI];
}

- (void)createUI {
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    UILabel *addLab = [[UILabel alloc] initWithFrame:CGRectMake(26, CGRectGetMaxY(self.nav.frame) + 66, 300, 30)];
    [self.view addSubview:addLab];
    addLab.text = @"昵称";
    addLab.textColor = [UIColor colorWithHexString:@"#221815"];
    addLab.font = PFRFontMedium(30);

    MXUserInfoModel *model = [DCCacheTool getCurrentUserInfo];
    UITextField *nameField = [[UITextField alloc] init];
    nameField.text = model.name;
    nameField.textColor = [UIColor colorWithHexString:@"#000000"];
    nameField.placeholder = @"请输入昵称";
    nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameField.font = PFRFont(14);
    nameField.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    nameField.layer.cornerRadius = 26;
    nameField.layer.masksToBounds = YES;
    UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 19, 52)];
    nameField.leftView = leftV;
    nameField.leftViewMode = UITextFieldViewModeAlways;
    [nameField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:nameField];
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.height.mas_equalTo(52);
        make.top.mas_equalTo(addLab.mas_bottom).offset(60);
    }];
    
    MJWeakSelf
    self.nextLab = [[UILabel alloc] init];
    [self.view addSubview:self.nextLab];
    self.nextLab.backgroundColor = [UIColor colorWithHexString:@"#3278F6"];
    self.nextLab.userInteractionEnabled = YES;
    self.nextLab.textColor = [UIColor whiteColor];
    self.nextLab.text = @"保存";
    self.nextLab.font = PFRFontMedium(16);
    self.nextLab.textAlignment = NSTextAlignmentCenter;
    self.nextLab.layer.cornerRadius = 24;
    self.nextLab.layer.masksToBounds = YES;
    [self.nextLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf finish];
    }]];
    
    [self.nextLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.right.offset(-14);
        make.height.mas_equalTo(48);
        make.bottom.offset(-87);
    }];
}

- (void)finish {
    if ([self.name isEqualToString:@""]) {
        [self.view xjmakeToast:@"请输入昵称"];
        return;
    }
    MXUserInfoModel *model = [DCCacheTool getCurrentUserInfo];
    NSDictionary *param = @{@"avatarUrl":@"",
                            @"username":self.name
    };
    
    MJWeakSelf
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/app/appUser/update" params:param httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        [weakSelf.view hideToastActivity];
        NSDictionary * dict = [responseObject mj_JSONObject];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            weakSelf.nav.backButton.userInteractionEnabled = NO;
            [weakSelf.view xjmakeToast:@"修改成功"];
            MXUserInfoModel *model = [DCCacheTool getCurrentUserInfo];
            model.name = weakSelf.name;
            [DCCacheTool saveUserInfo:model];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updatenicknamesuccess" object:nil];
            [weakSelf performSelector:@selector(backToTop) afterDelay:1.5];
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)backToTop {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textFieldDidChange:(UITextField *)textField {
    
    self.name = textField.text;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}

@end
